<!--
  * 客户订单表
  *
  * @Author:    dragon
  * @Date:      2023-12-11 13:27:12
  * @Copyright  dragon
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="姓名" class="smart-query-form-item">
        <a-input style="width: 150px" v-model:value="queryForm.studentName" placeholder="姓名"/>
      </a-form-item>
      <a-form-item label="订单状态" class="smart-query-form-item">
        <a-select style="width: 100%" allow-clear v-model:value="queryForm.orderStatus" placeholder="请选择订单状态">
          <a-select-option :value="0">待付款</a-select-option>
          <a-select-option :value="1">已付款</a-select-option>
          <a-select-option :value="2">已取消</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="queryData">
          <template #icon>
            <SearchOutlined/>
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined/>
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button @click="confirmBatchDelete" type="danger" size="small" :disabled="selectedRowKeyList.length == 0">
          <template #icon>
            <DeleteOutlined/>
          </template>
          批量删除
        </a-button>
        <a-button @click="exportOrder()" type="primary" size="small">
          <template #icon>
            <DownloadOutlined/>
          </template>
          导出
        </a-button>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="null" :refresh="queryData"/>
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table
        size="small"
        :dataSource="tableData"
        :columns="columns"
        rowKey="id"
        bordered
        :loading="tableLoading"
        :pagination="false"
        :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }"
    >
      <template #bodyCell="{ text, record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="orderSuccess(record.id)" v-show="record.orderStatus === 0" type="link">已付款</a-button>
            <a-button @click="orderCancel(record.id)" v-show="record.orderStatus === 0" type="link">取消订单</a-button>
            <a-button @click="orderDetail(record.id)" type="link">详情</a-button>
            <a-button @click="onDelete(record)" danger type="link">删除</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="queryData"
          @showSizeChange="queryData"
          :show-total="(total) => `共${total}条`"
      />
    </div>

    <OrderDetail ref="selectEmployeeModal"/>
  </a-card>
</template>
<script setup>
import OrderDetail from '/@/components/business/roland/order-detail/index.vue';

import {reactive, ref, onMounted} from 'vue';
import {message, Modal} from 'ant-design-vue';
import {SmartLoading} from '/@/components/framework/smart-loading';
import {rolandOrderApi} from '/@/api/business/roland-order/roland-order-api';
import {PAGE_SIZE_OPTIONS} from '/@/constants/common-const';
import {smartSentry} from '/@/lib/smart-sentry';
import TableOperator from '/@/components/support/table-operator/index.vue';
import {useRouter} from "vue-router";
// ---------------------------- 表格列 ----------------------------

const columns = ref([
  {
    title: 'id',
    dataIndex: 'id',
    ellipsis: true,
    width: 30
  },
  {
    title: '学生姓名',
    dataIndex: 'customerName',
    ellipsis: true,
    width: 80
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    ellipsis: false,
  },
  {
    title: '免费票数',
    dataIndex: 'freeNum',
    ellipsis: true,
    width: 80
  },
  {
    title: '限购票数',
    dataIndex: 'totalNum',
    ellipsis: true,
    width: 80
  },
  {
    title: '演出曲目',
    dataIndex: 'performanceType',
    ellipsis: false,
  },
  {
    title: '订单状态',
    dataIndex: 'orderStatusStr',
    ellipsis: true,
    width: 80
  },
  {
    title: '订单金额',
    dataIndex: 'amount',
    ellipsis: true,
    width: 80
  },
  {
    title: '付款时间',
    dataIndex: 'payTime',
    ellipsis: false,
    width: 170
  },
  {
    title: '下单时间',
    dataIndex: 'createTime',
    ellipsis: false,
    width: 170
  },
  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    width: 140,
  },
]);

// ---------------------------- 查询数据表单和方法 ----------------------------

const queryFormState = {
  studentName: undefined, //姓名
  orderStatus: undefined, //订单状态
  pageNum: 1,
  pageSize: 10,
};
// 查询表单form
const queryForm = reactive({...queryFormState});
// 表格加载loading
const tableLoading = ref(false);
// 表格数据
const tableData = ref([]);
// 总数
const total = ref(0);

// 重置查询条件
function resetQuery() {
  let pageSize = queryForm.pageSize;
  Object.assign(queryForm, queryFormState);
  queryForm.pageSize = pageSize;
  queryData();
}

// 查询数据
async function queryData() {
  tableLoading.value = true;
  try {
    let queryResult = await rolandOrderApi.queryPage(queryForm);
    tableData.value = queryResult.data.list;
    total.value = queryResult.data.total;
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}


onMounted(queryData);

// --------------------------- 订单详情 ---------------------------
let router = useRouter();

function orderDetail(orderId) {
  router.push({path: '/roland/order/order-detail', query: {orderId: orderId}});
}

// --------------------------- 导出 ---------------------------
async function exportOrder() {
  tableLoading.value = true;
  try {
    await rolandOrderApi.exportOrder(queryForm);
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}

// --------------------------- 已付款 ---------------------------
async function orderSuccess(orderId) {
  const form = {
    id: orderId,
    orderStatus: 1,
  };

  await rolandOrderApi.update(form);
  message.success(`操作成功`);
  resetQuery();
}

// --------------------------- 取消订单 ---------------------------
async function orderCancel(orderId) {
  const form = {
    id: orderId,
    orderStatus: 2,
  };

  await rolandOrderApi.update(form);
  message.success(`操作成功`);
  resetQuery();
}

// ---------------------------- 单个删除 ----------------------------
//确认删除
function onDelete(data) {
  Modal.confirm({
    title: '提示',
    content: '确定要删除吗?',
    okText: '删除',
    okType: 'danger',
    onOk() {
      requestDelete(data);
    },
    cancelText: '取消',
    onCancel() {
    },
  });
}

//请求删除
async function requestDelete(data) {
  SmartLoading.show();
  try {
    await rolandOrderApi.delete(data.id);
    message.success('删除成功');
    queryData();
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    SmartLoading.hide();
  }
}

// ---------------------------- 批量删除 ----------------------------

// 选择表格行
const selectedRowKeyList = ref([]);

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys;
}

// 批量删除
function confirmBatchDelete() {
  Modal.confirm({
    title: '提示',
    content: '确定要批量删除这些数据吗?',
    okText: '删除',
    okType: 'danger',
    onOk() {
      requestBatchDelete();
    },
    cancelText: '取消',
    onCancel() {
    },
  });
}

//请求批量删除
async function requestBatchDelete() {
  try {
    SmartLoading.show();
    await rolandOrderApi.batchDelete(selectedRowKeyList.value);
    message.success('删除成功');
    queryData();
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    SmartLoading.hide();
  }
}
</script>
